<?php
class IndexAction extends Action{
	//简单注册页面
	public function registerForm(){
		$this->display();
	}
	//详细注册页面
	public function detailForm(){
		$this->display();
	}
	//协议信息
	public function agreement(){
		$this->display();
	}
	//检查用户名是否存在
	public function checkUserName(){
		$username = $_POST['LoginEmail'];
		$onename = M('Users')->query("select UserId from __TABLE__ where UserName='{$username}'");
		if($onename){
			echo "1";
		}else{
			echo "0";
		}
	}
	//检查常用邮箱
	public function checkEmail(){
		$email = $this->_post('Email');
		$oneEmail = M('Users')->query("select UserId from __TABLE__ where Email='{$email}'");
		if($oneEmail){
			echo "1";
		}else{
			echo "0";
		}
	}
	//检查企业名称
	public function checkEnterpriseName(){
		$txtName = $this->_post('txtName');
		$oneName = M('Enterprise')->query("select Id from __TABLE__ where Name='{$txtName}'");
		if($oneName){
			echo "1";
		}else{
			echo "0";
		}
	}
	//检查行业名称
	public function checkIndustryName(){
		$industryName = $this->_post('industryName');
		$oneName = M('Industry')->query("select Id from __TABLE__ where Name='{$industryName}'");
		if($oneName){
			echo "1";
		}else{
			echo "0";
		}
	}
	//输出添加行业页面
	public function addIndustryInfo(){
		$this->display();
	}
	//添加行业名称
	public function addIndustry(){
		$Industry = M('Industry');
		$data['Name'] = $this->_post('industryName');
		$data['CreateDate'] = date('Y-m-d H:i:s');
		$data['IsPublic'] = 1;
		$insertid = $Industry->data($data)->add();
		if($insertid){
			echo '<script>window.parent.insertres(true,"'.$insertid.'","'.$this->_post('industryName').'");</script>';
		}else{
			echo '<script>window.parent.insertres(false,null,null);</script>';
		}
	}
	//输出添加企业页面
	public function addEnterpriseInfo(){
		$this->display();
	}
	//添加企业名称
	public function addEnterprise(){
		$Enterprise = M('Enterprise');
		$data['Name'] = $this->_post('txtName');
		$data['Introduce'] = $this->_post('txtIntroduce');
		$data['LinkMan'] = $this->_post('txtLinkman');
		$data['TelePhone'] = $this->_post('txtPhone');
		$data['WebUrl'] = $this->_post('txtUrl');
		$data['IsPublic'] = 1;
		//处理上传的Logo
		import('ORG.Net.UploadFile');
			$upload = new UploadFile();// 实例化上传类
			$upload->maxSize  = 1024000 ;// 设置附件上传大小1M
			$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath =  './UpFile/Img/EnterpriseLogo/';// 设置附件上传目录
			//设置需要生成缩略图，仅对图像文件有效
			$upload->thumb = true;
			// 设置引用图片类库包路径
			$upload->imageClassPath = 'ORG.Util.Image';
			//设置需要生成缩略图的文件前缀
			$upload->thumbPrefix = 'entlogo_';  //产生1张缩略图
			//设置缩略图最大宽度
			$upload->thumbMaxWidth = '120';
			//设置缩略图最大高度
			$upload->thumbMaxHeight = '50';
			//设置上传文件规则
			$upload->saveRule = uniqid;
			//删除原图
			$upload->thumbRemoveOrigin = true;
			if(!$upload->upload()){
				// 上传错误提示错误信息
				$this->error($upload->getErrorMsg());
			}else{
				// 上传成功 获取上传文件信息
				$info = $upload->getUploadFileInfo();
				$data['Logo'] = $info[0]['savename'];
			}
		$insertid = $Enterprise->data($data)->add();
		if($insertid){
			echo '<script>window.parent.insertres(true,"'.$insertid.'","'.$this->_post('txtName').'");</script>';
		}else{
			echo '<script>window.parent.insertres(false,null,null);</script>';
		}
	}
	//教官简单注册信息写入
	public function registerCom(){
		$form['UserType'] = 4;
		$form['UserName'] = trim($this->_post('LoginEmail'));
		$arr = $this->_post('IndustryList');
		$IndustryId = join(',',$arr);
		$form['Password'] = md5(trim($this->_post('Password')));
		$form['OrgName'] = $this->_post('EnterpriseName');
		$EnterpriseId = $this->_post('EnterpriseList');
		$form['TrueName'] = $this->_post('TrueName');
		$form['PhoneNumber'] = $this->_post('PhoneNumber');
		$form['Email'] = $this->_post('Email');
		$form['CreateDate'] = date('Y-m-d H:i:s');
		$form['IsApproved'] = 0;
		$add = create_user($form);
		$_SESSION[userId]=$add;
		if($add){
			$userId = $add;
			//操作teacher表
			$teacher = D('Teacher');
			$result = $teacher->create();
			if (!!$result){
				// 如果创建失败 表示验证没有通过 输出错误提示信息
				echo '数据写入失败，请与我们联系';

			}else{
				// 验证通过 进行其他数据操作
				$teacher->execute("insert into __TABLE__ (UserId,IndustryId,EnterpriseId) values ('{$userId}','{$IndustryId}','{$EnterpriseId}')");
				$this->success('基本信息已提交', "detailForm");
			}
		}else{
			$this->error('提交失败');
		 }
	}
	//获取行业信息
	public function getIndustryList(){
		$Industry = M('Industry');
		$IndustryList = $Industry->query("select Id,Name from __TABLE__ where IsPublic=1");
		echo json_encode($IndustryList);
	}
	//获取企业业信息
	public function getEnterpriseList(){
		$Enterprise = M('Enterprise');
		$EnterpriseList = $Enterprise->query("select Id,Name from __TABLE__ where IsPublic=1");
		echo json_encode($EnterpriseList);
	}
	//教官详细信息注册
	public function detailFormCom(){
	    $uid = $_SESSION['userId'];
		$data=array(
				"DepartmentName" => "{$this->_post('DepartmentName')}",
				"Job" => "{$this->_post('Job')}",
				"Title" => "{$this->_post('Title')}",
				"Address" => "{$this->_post('Address')}-{$this->_post('Address1')}-{$this->_post('Address2')}-{$this->_post('Address3')}",
				"PostalCode" => "{$this->_post('PostalCode')}",
				"FixedPhone" => "{$this->_post('Telphone')}-{$this->_post('Tel_number')}-{$this->_post('Tel_ext')}",
				"OtherPhone" => "{$this->_post('OtherPhone')}",
				"MSN" => "{$this->_post('MSN')}",
				"QQ" => "{$this->_post('QQ')}",
				"Fetion" => "{$this->_post('Fetion')}",
				"PersonalIntroduce" => "{$this->_post('PersonalIntroduce')}",
			);dump($data);
		$m = M('teacher')->where("UserId={$uid}")->setField($data);
		if($m){
			$this->success('注册成功，请等待审核',"__APP__/");
		}else{
			$this->error('提交失败');
		}
	}
	//添加修改基本信息页面
	public function editRegister(){
		$uid = $_SESSION['userId'];
		$user = M('Users')->where("UserId={$uid}")->field('UserName,TrueName,PhoneNumber,Email')->find();
		$this->assign('user',$user);
		$this->display();
	}
	//更新修改后的基本信息
	public function editRegisterForm(){
		$uid = $_SESSION['userId'];
		$pass = md5(trim($this->_post('Password')));
		$arr = $this->_post('IndustryList');
		$IndustryId = join(',',$arr);
		$EnterpriseId = $this->_post('EnterpriseList');
		$form = array(
			"IndustryId" => "{$IndustryId}",
			"EnterpriseId" => "{$EnterpriseId}",
		);
		$data = array(
			"Password" => "{$pass}",
			"TrueName" => "{$this->_post('TrueName')}",
			"PhoneNumber" => "{$this->_post('PhoneNumber')}",
			"Email" => "{$this->_post('Email')}",
		);
		edit_base_info($uid,$data);
		M('teacher')->where("UserId={$uid}")->setField($form);
		$this->success('基本信息已修改', "detailForm");
	}
}